import { Http } from '@/network/request'
import { SubmitDTO } from './data'
import { ElMessage, ElMessageBox } from 'element-plus' 

// 提交
export const saveOrUpdate = (data: SubmitDTO) => {
  return Http.post<typeof data, null>('/codeDuckUser/saveOrUpdate', data, { typeStr: data.id ? '更新' : '新增' })
}

// 重置密码方法
export const resetPasswordHandle = async (id: string) => {
  try {
    await ElMessageBox.confirm('是否重置默认密码为123456？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })

    const res = await Http.post('/codeDuckUser/resetPassword', {
      id,
      newPassword: '123456'
    })

    if (res.code === 0) {
      ElMessage.success('密码重置成功')
    }
  } catch (error) {
    // 用户点击取消时不处理
    if (error !== 'cancel') {
      ElMessage.error('密码重置失败')
    }
  }
}
